//
//  WXWKWebViewController.m
//  TestPayWeb
//
//  Created by MJL on 2018/3/21.
//  Copyright © 2018年 MJL. All rights reserved.
//

#import "WXWKWebViewController.h"
#import <WebKit/WebKit.h>
#import "WebChatPayH5VIew.h"
#import "SGScanningQRCodeVC.h"
#import <AVFoundation/AVFoundation.h>
#import "SGAlertView.h"
#import <Photos/Photos.h>
#import "WEBModel.h"
#import <WebKit/WebKit.h>
@interface WXWKWebViewController ()<WKNavigationDelegate,WKUIDelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate>

@property (strong, nonatomic) WKWebView *myWebView;
@property(strong,nonatomic)UIImageView *imageView;


@property(nonatomic,strong)NSURL *url;

@property(nonatomic,strong)UIView *beiJingview,*bottomView;
@property(nonatomic,strong)NSArray<WEBModel *> *tableArr;
@property(nonatomic,strong)UIProgressView *progressView;
@property(nonatomic,strong)UIBarButtonItem *pulishButton2;

@property(nonatomic,strong)UIScrollView *scrollView;


@property(nonatomic,assign)BOOL isShanChu;

/** 保存当前请求的URL */
@property(nonatomic, strong) NSURL *currentUrl;

@end

@implementation WXWKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isShanChu=false;
    
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(networkDidReceiveMessage:) name:@"saoyisao" object:nil];
    

    self.myWebView = [[WKWebView alloc] initWithFrame:self.view.frame];
    self.myWebView.navigationDelegate = self;
    self.myWebView.UIDelegate=self;
    [self.view addSubview:self.myWebView];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1;
    longPress.delegate = self;
    [self.myWebView addGestureRecognizer:longPress];
    

    //加载h5链接
    NSURL *url = [NSURL URLWithString:@"https://tyl.xinjiazhou.cn"];
    
 
  
    
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, CGRectGetWidth(self.view.frame), 2)];
    self.progressView.progressTintColor = [UIColor orangeColor];
    [self.view addSubview:self.progressView];

    
    _imageView=[UIImageView new];
    [self.view addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(iPhoneX ? 48.f : 20.f);
        make.bottom.mas_equalTo(iPhoneX ? 34.f : 0);
    }];

    _imageView.image=[UIImage imageNamed:@"画板"];
    self.navigationController.navigationBarHidden=YES;

    
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
    
    [self.myWebView loadRequest:mutableRequest];

    
    [self.myWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    [self.myWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    
    UIBarButtonItem *pulishButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:pulishButton,nil]];
    
    
    _pulishButton2=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"矩形"] style:UIBarButtonItemStylePlain target:self action:@selector(creatMoerView)];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:_pulishButton2,nil]];
    [self getData];
}


- (void)handleLongPress:(UILongPressGestureRecognizer *)sender{
    
    

    if (sender.state != UIGestureRecognizerStateBegan) {
        return;
    }
    CGPoint touchPoint = [sender locationInView:self.myWebView];
    // 获取长按位置对应的图片url的JS代码
    NSString *imgJS = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
    // 执行对应的JS代码 获取url
    [self.myWebView evaluateJavaScript:imgJS completionHandler:^(id _Nullable imgUrl, NSError * _Nullable error) {
       
        if (imgUrl) {
            

            NSString *userAgent = [NSString stringWithFormat:@"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"];
            SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
            [manager setValue:userAgent forHTTPHeaderField:@"User-Agent"];
            
            [manager downloadImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imgUrl]] options:SDWebImageDownloaderProgressiveDownload progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                
                UIImage *image2=[UIImage imageWithData:data];
               
                
        
                if(image2){
                    //1. 初始化扫描仪，设置设别类型和识别质量
                    CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
                    //2. 扫描获取的特征组
                    NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image2.CGImage]];
                    //3. 获取扫描结果
                    if (features.count==0) {
                        
                        UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle: @"" message: @"操作" preferredStyle: UIAlertControllerStyleActionSheet];
                        
                        [actionSheetController addAction: [UIAlertAction actionWithTitle:@"保存图片"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            
                            UIImageWriteToSavedPhotosAlbum(image2,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
                            
                            
                        }]];
//                        [actionSheetController addAction: [UIAlertAction actionWithTitle: @"识别二维码" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {

//                        }]];
                        
                        [actionSheetController addAction: [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnull action) {}]];
                        
                        [self presentViewController: actionSheetController animated:YES completion:^{}];
                    }
                    else{
                        
                        UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle: @"" message: @"操作" preferredStyle: UIAlertControllerStyleActionSheet];
                        
                        [actionSheetController addAction: [UIAlertAction actionWithTitle:@"保存图片"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            
                            UIImageWriteToSavedPhotosAlbum(image2,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
                            
                            
                        }]];
                        [actionSheetController addAction: [UIAlertAction actionWithTitle: @"识别二维码" style: UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
                                CIQRCodeFeature *feature = [features objectAtIndex:0];
                                NSString *scannedResult = feature.messageString;
                            
                            NSURL *url=[NSURL URLWithString:scannedResult];
                            
                            NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
                            
                            [self.myWebView loadRequest:mutableRequest];
                            
                        
                            }]];
                        
                        [actionSheetController addAction: [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnull action) {}]];
                        
                        [self presentViewController: actionSheetController animated:YES completion:^{}];
                        
                    }
                  
                }
                
            }];
            
           
            
           
            
            
          
           
        }
    }];
    
    



    
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
        
    }else{
        msg = @"保存图片成功" ;
        
    }
    [gongju svpInfo:msg];
}

//可以识别多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}  

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [_imageView removeFromSuperview];
    self.navigationController.navigationBarHidden=NO;
}


#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    
    
    NSLog(@"1");
    
    NSURL *URL = navigationAction.request.URL;
    

    
    
    NSString *scheme = [URL scheme];
    UIApplication *app = [UIApplication sharedApplication];
    
    NSLog(@"%@",navigationAction.request.URL);
    
    // 打电话
    if ([scheme isEqualToString:@"tel"]) {
            if ([app canOpenURL:URL]) {
                [app openURL:URL];
                // 一定要加上这句,否则会打开新页面
                decisionHandler(WKNavigationActionPolicyCancel);
                return;
            }
        }

    NSString *url = navigationAction.request.URL.absoluteString;
    

    

    if ([url containsString:@"https://wx.tenpay.com"]) {
  
      
    }else if ([url containsString:@"about:blank"]) {
    
        
    }else{
          _url=navigationAction.request.URL;
    }
    
    if ([url containsString:@"https://wx.tenpay.com"]) {
#warning 微信支付链接不要拼接redirect_url，如果拼接了还是会返回到浏览器的
        //传入的是微信支付链接：https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx201801291021026cb304f9050743178155&package=3456576571
        //这里把webView设置成一个像素点，主要是不影响操作和界面，主要的作用是设置referer和调起微信
        
        decisionHandler(WKNavigationActionPolicyCancel);
        
        WebChatPayH5VIew *h5View = [[WebChatPayH5VIew alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        //url是没有拼接redirect_url微信h5支付链接
        h5View.url=_url;
        [h5View loadingURL:url withIsWebChatURL:NO];
        [self.view addSubview:h5View];
        
           
        return;
    }
    if (![url containsString:@"https://"]&&![url containsString:@"http://"]) {
        [[UIApplication sharedApplication] openURL:URL];
    }
    


    WKNavigationResponsePolicy actionPolicy = WKNavigationActionPolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);

    
}



// 在收到响应开始加载后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
{

    NSString *urlStr = [navigationResponse.response.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


    if ([urlStr containsString:@"alipays://"]) {
        
        NSRange range = [urlStr rangeOfString:@"alipays://"]; //截取的字符串起始位置
        NSString * resultStr = [urlStr substringFromIndex:range.location]; //截取字符串

        NSURL *alipayURL = [NSURL URLWithString:resultStr];

        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {

            }];
        } else {

        }
    }
    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
}
-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {

    completionHandler();
}




-(void)networkDidReceiveMessage:(NSNotification *)notification{
    NSDictionary *dic=notification.userInfo;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"saoyisao" object:nil];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[dic objectForKey:@"scannedResult"]]];
    [request addValue:@"755ccad2ec0bb3d0fce3e7405e166e4a" forHTTPHeaderField:@"Tyl-Built-Access"];
     [self.myWebView loadRequest:request];
}

-(void)getData{
    [gongju AFNPOSTDataUrl:@"http://tyl_app.xinjiazhou.cn/api/client/basic_menu.html" andDic:nil andTableView:nil andView:nil andSuccessBlock:^(id responseObject) {
  
        self->_tableArr=responseObject;
        
    } anderrorBlock:nil];
}
-(void)back{
    if ([self.myWebView canGoBack]) {
        [self.myWebView goBack];
    }
    
}
-(void)back2{
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://tyl.xinjiazhou.cn"]];
    [self.myWebView loadRequest:request];
    
}




- (void)dealloc{
    [self.myWebView removeObserver:self forKeyPath:@"title"];
    [self.myWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}

- (void)clearWebCache {
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        
        NSSet *websiteDataTypes= [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeMemoryCache,]];
        
        
        
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        
        
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
            
            [gongju svpInfo:@"清除缓存成功"];
            
        }];
    } else {
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
        
        NSError *errors;
        
        BOOL remove=[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
        if (remove) {
            [gongju svpInfo:@"清除缓存成功"];
        }
    }
}

#pragma mark KVO的监听代理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    //加载进度值
    if ([keyPath isEqualToString:@"title"]){//网页title
        if (object == self.myWebView){
            self.navigationItem.title = self.myWebView.title;
        }else{
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }else if ([keyPath isEqual:@"estimatedProgress"] && object == self.myWebView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.myWebView.estimatedProgress animated:YES];
        if (self.myWebView.estimatedProgress  >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:YES];
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

-(void)creatMoerView{
    
    _pulishButton2.enabled=NO;
   
    if (_isShanChu) {
        [self shanchu];
        return;
    }
    _scrollView=[UIScrollView new];
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    _scrollView.scrollsToTop = NO;
    _scrollView.backgroundColor=[UIColor clearColor];
    _scrollView.contentSize = CGSizeMake(0, 0);
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.scrollEnabled = NO;
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.pagingEnabled=YES;
    _scrollView.delegate=self;
    
    
    
    _beiJingview=[UIView new];
    [_scrollView addSubview:_beiJingview];
    [_beiJingview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(_mainH+181);
        make.width.mas_equalTo(_mainW);
    }];
    _beiJingview.backgroundColor=[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.4];
    
    [_scrollView setContentOffset:CGPointMake(0, 181) animated:YES];
    
    _bottomView=[UIView new];
    [_beiJingview addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(181);
    }];
    _bottomView.backgroundColor=[UIColor whiteColor];
    
    UIView *topView=[UIView new];
    [_beiJingview addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self->_bottomView.mas_top);
    }];
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shanchu)];
    [topView addGestureRecognizer:tapGesture2];
    
    UIView *xianView=[UIView new];
    [_bottomView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.center.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=ColorWithRGB(29, 29, 29);
    
    NSArray *arr1=[NSArray arrayWithObjects:@"刷新",@"扫一扫",@"清除缓存", nil];
    
    for (int i=0; i<3; i++) {
        UIView *beiJingView=[UIView new];
        [_bottomView addSubview:beiJingView];
        [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10-(_mainW-20)/5*i);
            make.width.mas_equalTo((_mainW-20)/5);
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(90);
        }];
        beiJingView.tag=i;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(GongNeng:)];
        [beiJingView addGestureRecognizer:tapGesture];
        
        UIImageView *imageView=[UIImageView new];
        [beiJingView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(-10);
            make.height.width.mas_equalTo(30);
        }];
        
        imageView.image=[UIImage imageNamed:arr1[i]];
        
        UILabel *label=[UILabel new];
        [beiJingView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(imageView.mas_bottom).mas_equalTo(8);
        }];
        label.text=arr1[i];
        label.font=[UIFont systemFontOfSize:13];
        label.textColor=ColorWithRGB(29, 29, 29);
    }
    
    for (int i=0; i<_tableArr.count; i++) {
        WEBModel *mode=[WEBModel mj_objectWithKeyValues:_tableArr[i]];
        UIView *beiJingView=[UIView new];
        [_bottomView addSubview:beiJingView];
        [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10+(_mainW-20)/5*i);
            make.width.mas_equalTo((_mainW-20)/5);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(90);
        }];
        
        beiJingView.tag=i;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushTOMore:)];
        [beiJingView addGestureRecognizer:tapGesture];
        
        
        
        
        UIImageView *imageView=[UIImageView new];
        [beiJingView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(-10);
            make.height.width.mas_equalTo(30);
        }];

        NSString *ico=mode.ico;
       
        NSString *userAgent = [NSString stringWithFormat:@"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"];
        SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
        [manager setValue:userAgent forHTTPHeaderField:@"User-Agent"];
       
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:ico]  placeholderImage:nil options:SDWebImageTransformAnimatedImage];
        
 
        UILabel *label=[UILabel new];
        [beiJingView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(imageView.mas_bottom).mas_equalTo(8);
        }];
        label.text=mode.title;
        label.font=[UIFont systemFontOfSize:13];
        label.textColor=ColorWithRGB(29, 29, 29);
    }

    
}

-(void)GongNeng:(UITapGestureRecognizer *)tap{
    _isShanChu=true;
    [self shanchu];
    
    if (tap.view.tag==0) {
        [self.myWebView reload];
    }
    if (tap.view.tag==1) {
        [self scanningQRCode];
    }
    if (tap.view.tag==2) {
        [self clearWebCache];
    }
    
    
}
-(void)pushTOMore:(UITapGestureRecognizer *)tap{
    _isShanChu=true;
    [self shanchu];

    WEBModel *mode=[WEBModel mj_objectWithKeyValues:_tableArr[tap.view.tag]];
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:mode.url]];
    
    
    
    [self.myWebView loadRequest:request];
}

-(void)shanchu{
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

    
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if (_isShanChu) {
        [_scrollView removeFromSuperview];
        _isShanChu=false;
    }
    else{
       _isShanChu=true;
    }

     _pulishButton2.enabled=YES;

}



- (void)scanningQRCode {
    // 1、 获取摄像设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device) {
        // 判断授权状态
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted) { // 因为家长控制, 导致应用无法方法相册(跟用户的选择没有关系)
            //            [self alertTitle:@"因为系统原因, 无法访问相册"];
        } else if (status == PHAuthorizationStatusDenied) { // 用户拒绝当前应用访问相册
            SGAlertView *alertView = [SGAlertView alertViewWithTitle:@"⚠️ 警告" delegate:nil contentTitle:@"请去-> [设置 - 隐私 - 照片 - 陶乐优] 打开访问开关" alertViewBottomViewType:(SGAlertViewBottomViewTypeOne)];
            [alertView show];
        } else if (status == PHAuthorizationStatusAuthorized) { // 用户允许当前应用访问相册
            SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
            [self.navigationController pushViewController:scanningQRCodeVC animated:YES];
        } else if (status == PHAuthorizationStatusNotDetermined) { // 用户还没有做出选择
            // 弹框请求用户授权
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) { // 用户点击了好
                    
                }
            }];
        }
        
    } else {
        SGAlertView *alertView = [SGAlertView alertViewWithTitle:@"⚠️ 警告" delegate:nil contentTitle:@"未检测到您的摄像头, 请在真机上测试" alertViewBottomViewType:(SGAlertViewBottomViewTypeOne)];
        [alertView show];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
